package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import fact.view.DTO.ScenarioDTO;
import fact.view.DTO.SegmentDTO;
import fact.view.model.ScenarioModelBean;
import fact.view.model.SegmentModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;

@ManagedBean(name = "scenarioBean")
@RequestScoped
public class ScenarioBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	
	
	@ManagedProperty(value="#{scenarioModelBean}")
	private ScenarioModelBean scenarioModelBean;
	
	ArrayList<ScenarioDTO> scenarioList=new  ArrayList<ScenarioDTO>();

	private ScenarioDTO scenario=new ScenarioDTO();
	private ScenarioDTO editScenario=new ScenarioDTO();
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
	public String addScenario(ActionEvent event) throws SQLException
	{
		opened=false;
		editOpened=false;
		
		if(this.scenario.getScenarioName()==null || this.scenario.getScenarioName().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:scenarioName","Scenario Name cannot be empty.");
		}
		
		
		if(this.scenario.getPosition()==0)
		{
			opened=true;			
			FacesUtils.addErrorMessage("form:scenarioPosition","Scenario Position should be a number and cannot be zero.");
		}
		
		
		if(opened==false)
		{
			if(scenarioModelBean.getScenario(this.scenario.getScenarioName().trim())!=null)
			{
				opened=true;
				
				FacesUtils.addErrorMessage("form:scenarioName","Scenario Name Already Exist.");
			}
			else
			{
				if(scenarioModelBean.addScenario(this.scenario.getScenarioName(),this.scenario.getPosition())==true)
				{
					scenarioList=scenarioModelBean.getScenarioList();
					session.setAttribute("session_scenarioList", scenarioList);
					
					FacesUtils.addInfoMessage(null,"Scenario has successfully been added.");
					return "admin/scenario.jsf";
	
				}
				
			}
		}
		return null;
	}
	
	public String deleteScenario() throws SQLException
	{
		System.out.println("Scenario Id :"+this.scenario.getScenarioId());
		if(scenarioModelBean.deleteScenario(this.scenario.getScenarioId())==true)
		{
			scenarioList=scenarioModelBean.getScenarioList();
			session.setAttribute("session_scenarioList", scenarioList);
			
			FacesUtils.addInfoMessage(null,"Scenario has successfully been deleted.");
		}
		return "admin/scenario.jsf";
	}
	
	 
	public String editScenario() throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.editScenario.getScenarioName()==null || this.editScenario.getScenarioName().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editScenarioName","Scenario Name cannot be empty.");
		}
		
		
		if(this.editScenario.getPosition()==0)
		{
			editOpened=true;			
			FacesUtils.addErrorMessage("form:editScenarioPosition","Scenario Position should be a number and cannot be zero.");
		}
		
		if(editOpened==false)
		{
			System.out.println("Scenario Id :"+this.editScenario.getScenarioId());
			if(scenarioModelBean.editScenario(this.editScenario.getScenarioName(),this.editScenario.getPosition(),this.editScenario.getScenarioId())==true)
			{
				scenarioList=scenarioModelBean.getScenarioList();
				session.setAttribute("session_scenarioList", scenarioList);
				
				FacesUtils.addInfoMessage(null,"Scenario has successfully been edited.");
			}
			return "admin/scenario.jsf";
		}
		return null;
	}

	

	public ScenarioModelBean getScenarioModelBean() {
		return scenarioModelBean;
	}

	public void setScenarioModelBean(ScenarioModelBean scenarioModelBean) {
		this.scenarioModelBean = scenarioModelBean;
	}

	public ArrayList<ScenarioDTO> getScenarioList() {
		try{
			
			if(session.getAttribute("session_scenarioList")==null)
			{
				scenarioList=scenarioModelBean.getScenarioList();
				session.setAttribute("session_scenarioList", scenarioList);
			}
			else
				scenarioList=(ArrayList<ScenarioDTO>)session.getAttribute("session_scenarioList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return scenarioList;
	}
	
	public ScenarioDTO getScenario() {
		return scenario;
	}

	public void setScenario(ScenarioDTO scenario) {
		this.scenario = scenario;
	}

	public ScenarioDTO getEditScenario() {
		return editScenario;
	}

	public void setEditScenario(ScenarioDTO editScenario) {
		this.editScenario = editScenario;
	}

	public void setScenarioList(ArrayList<ScenarioDTO> scenarioList) {
		this.scenarioList = scenarioList;
	}

	/** -- Popup Modal Event ---**/
		public boolean isOpened() { return opened; }
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setOpened(boolean opened) { this.opened = opened; }
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEvent(ActionEvent event) {
	    	this.scenario=null;
	        opened = true;
	        editOpened = false;
	    }
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	this.editScenario=scenarioModelBean.getScenario(this.editScenario.getScenarioId());
	    	editOpened = true;
	    	opened = false;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        opened = false;
	        editOpened = false;
	    }

    /*  End of Modal */
	

}
